import { _decorator, Component, Label, Node } from 'cc';
import { Props3Action } from './Props3Action';
import { Props1Action } from './Props1Action';
import { Props2Action } from './Props2Action';
import { Props4Action } from './Props4Action';
import { CatAction } from './CatAction';
import { Global } from './Global';
import { AdManger } from './ad/AdManger';
import { BasePage } from './BasePage';

const { ccclass, property } = _decorator;

@ccclass('PropsAction')
export class PropsAction extends BasePage {

    @property(Props1Action)
    props1_Action: Props1Action = null!;
    @property(Props2Action)
    props2_Action: Props2Action = null!;
    @property(Props3Action)
    props3_Action: Props3Action = null!;
    @property(Props4Action)
    props4_Action: Props4Action = null!;

    @property(Label)
    coins_label: Label = null!;

    @property(Node)
    btn_close: Node = null!;
    @property(Node)
    btn_continue: Node = null!;
    @property([Node])
    btns_arr: Node[] = [];

    // 用数组管理所有 props 节点，方便统一操作
    private propsNodes: Node[] = [];

    start() {
        super.start();
        // 初始化所有 props 节点数组
        this.propsNodes = [
            this.props1_Action.node,
            this.props2_Action.node,
            this.props3_Action.node,
            this.props4_Action.node,
        ];
    }

    update(deltaTime: number) {
        // 可添加更新逻辑，当前为空
    }

    open(btn_num: number = 0) {
        super.open();
        this.set_all_props_inactive();

        // 激活对应 props，注意传入对应的数字
        switch (btn_num) {
            case 1:
                this.props1_Action.node.active = true;
                this.props1_Action.open();
                break;
            case 2:
                this.props2_Action.node.active = true;
                this.props2_Action.open();
                break;
            case 3:
                this.props3_Action.node.active = true;
                this.props3_Action.open();
                break;
            case 4:
                this.props4_Action.node.active = true;
                this.props4_Action.open();
                break;
        }

        this.coins_label.string = Global.cur_coins.toString();

        // 延迟激活按钮统一处理，减少重复代码
        this.schedule_activation(this.btn_close, 3);
        this.schedule_activation(this.btn_continue, 2);

        // 延迟激活按钮组
        if (this.btns_arr.length > 0) {
            this.btns_arr.forEach(btn => btn.active = false);
            this.scheduleOnce(() => {
                this.btns_arr.forEach(btn => btn.active = true);
            }, 1.5);
        }
    }

    show_box(cat_action: CatAction) {
        this.open(4);
        this.props4_Action.show_box(cat_action);
    }

    private set_all_props_inactive() {
        this.propsNodes.forEach(node => node.active = false);
    }

    private schedule_activation(node: Node | null, delay: number) {
        if (!node) return;
        node.active = false;
        this.scheduleOnce(() => {
            node.active = true;
        }, delay);
    }

    close() {
        this.set_all_props_inactive();
        super.close();
        AdManger.show_interstial();
    }
}
